from my_base_service import *
from my_prompt_template import *
import uuid
from langfuse.client import Langfuse

need_answer_chain = (
        need_answer
        | get_model()
        | strParser
)

is_duplicated_chain = (
        check_duplicated
        | get_model()
        | strParser
)




# 创建一个新trace
def create_trace(user_id):
    langfuse = Langfuse()
    # 创建一个不重复的 id
    trace_id = str(uuid.uuid4())
    trace = langfuse.trace(
        name="agiclass_assistant",
        id=trace_id,
        user_id=user_id
    )
    return trace


# 主流程
def verify_question(
        question: str,
        outlines: str,
        question_list: list,
        user_id: str,
) -> bool:
    trace = create_trace(user_id)
    handler = trace.get_langchain_handler()
    # 判断是否需要回答
    if need_answer_chain.invoke(
            {"user_input": question, "outlines": outlines},
            config={"callbacks": [handler]}
    ) == 'Y':
        # 判断是否为重复问题
        if is_duplicated_chain.invoke(
                {"user_input": question,
                 "question_list": "\n".join(question_list)},
                config={"callbacks": [handler]}
        ) == 'N':
            question_list.append(question)
            return True
    return False


# 实际调用
ret = verify_question(
    "LangChain支持Java吗",
    # "老师好",
    outlines,
    question_list,
    user_id="roc",
)
print(ret)
